[% setvar title UNIVERSAL::import() %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>UNIVERSAL::import()</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Michael G Schwern &lt;<a href='mailto:schwern@pobox.com'>schwern@pobox.com</a>&gt;
  Date: 18 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 257
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>UNIVERSAL::import() will be a flyweight method to provide very
simple semantics to export variables and functions.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Exporter is big and complicated.  It is so big it has been split up
into Exporter and Exporter::Heavy.  Its so complicated that it takes
264 lines to explain it.</p>
<p>Most of the time, you just want to export some variables and methods.
This part of Exporter is almost trivial to implement that people often
write their own simple import() routines.</p>
<p>UNIVERSAL::import() would take this basic Exporter::import()
functionality and implement it in a very lean fashion.  It would
support only three of Exporter::import's features.</p>
<pre>    @EXPORT      - A default list of symbols to export
    @EXPORT_OK   - A list of symbols to export upon request
    @EXPORT_FAIL - A list of symbols which cannot be exported</pre>
<p>Tags, &quot;/pattern/&quot;, negation, export_to_level(), export_fail() and all
supporting paraphernalia are all left to Exporter.  Module version
checking is left to UNIVERSAL::require.</p>
<p>Removal of the more obscure features should make basic exporting
faster and easier to document.  So too would it be made much more
efficient if implemented in universal.c.</p>
<a name='UNIVERSAL::import() is only a class method.'></a><h2>UNIVERSAL::import() is only a class method.</h2>
<p>Because it is strongly recommended that you not export methods,
import() makes little sense as an object method.  If you have an
object for a module, more than likely you're not going to be exporting
functions.  <code>$obj-</code>import()&gt; should be an explicit run-time error.</p>
<p>This protects against the programming mistake where $class is
accidentally an object.  <code>$class-</code>import&gt; would silently work, making
the mistake difficult to find.  This case should be far more common
than that of desiring to import a module based on an object.</p>
<p>if import() is desired as an object method, it can be easily overridden with:</p>
<pre>    sub import {
        my $proto = shift;
        my $class = ref $proto || $proto;
        return $class-&gt;SUPER::import(@_);
    }</pre>
<p>Ta da.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>Replace UNIVERSAL::import() with a method that dies with a q|Can't
locate object method &quot;%s&quot; via package &quot;%s&quot;| so that Class-&gt;import()
continues to fail if Class does not implement import() or subclass
Exporter.</p>
<p>Class-&gt;can('import') remains a problem.  UNIVERSAL::can may have to be
coded with a special case to cause a failure in Perl5 code.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>A complete Perl 5 implementation follows:</p>
<pre>    package UNIVERSAL;
    
    sub import {
        my($exporter, @imports)  = @_;
        my($caller, $file, $line) = caller;
    
        # XXX Can't tell difference between &quot;use Module&quot; and &quot;use Module ()&quot;
        unless( @imports ) {        # Default import.
            @imports = @{$exporter.'::EXPORT'};
        }
        else {
            if( @{$exporter.'::EXPORT_FAIL'} ) {
                # This can be cached.
                my %fail = map { $_ =&gt; 1 } @{$exporter.'::EXPORT_FAIL'};
    
                my($denied) = grep {$fail{$_}} @imports;
                _not_exported($denied, $exporter, $file, $line) if $denied;
            }
    
            # Because @EXPORT_OK = () would indicate that nothing is
            # to be exported, we cannot simply check the length of @EXPORT_OK.
            # We must to oddness to see if the variable exists at all as
            # well as avoid autovivification.
            # XXX idea stolen from base.pm, this might be all unnecessary
            my $eokglob;
            if( $eokglob = ${$exporter.'::'}{EXPORT_OK} and *$eokglob{ARRAY} ) {
                if( @{$exporter.'::EXPORT_OK'} ) {
                    # This can also be cached.
                    my %ok = map { $_ =&gt; 1} @{$exporter.'::EXPORT_OK'};
    
                    my($denied) = grep {!$ok{$_}} @imports;
                    _not_exported($denied, $exporter, $file, $line) if $denied;
                }
                else {      # We don't export anything.
                    _not_exported($imports[0], $exporter, $file, $line);
                }
            }
           
        }
    
        _export($caller, $exporter, @imports);
    }
    
    sub _export {
        my($caller, $exporter, @imports) = @_;
    
        # Stole this from Exporter::Heavy.  I'm sure it can be written better
        # but I'm lazy at the moment.
        foreach $sym (@imports) {
            # shortcut for the common case of no type character
            (*{&quot;${caller}::$sym&quot;} = \&amp;{&quot;${exporter}::$sym&quot;}, next)
                unless $sym =~ s/^(\W)//;
            my $type = $1;
            *{&quot;${caller}::$sym&quot;} =
                $type eq '&amp;' ? \&amp;{&quot;${exporter}::$sym&quot;} :
                $type eq '$' ? \${&quot;${exporter}::$sym&quot;} :
                $type eq '@' ? \@{&quot;${exporter}::$sym&quot;} :
                $type eq '%' ? \%{&quot;${exporter}::$sym&quot;} :
                $type eq '*' ?  *{&quot;${exporter}::$sym&quot;} :
                do { require Carp; Carp::croak(&quot;Can't export symbol: $type$sym&quot;) };
        }
    }
    
    sub _not_exported {
        my($thing, $exporter, $file, $line) = @_;
        die sprintf qq|&quot;%s&quot; is not exported by the %s module at %s line %d\n|,
            $thing, $exporter, $file, $line;
    }
    
    1;</pre>
<p>Of course, the efficiency of the implementation can be tightened up.
Its just a prototype.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 233 - Replace Exporter with a better scaling mechanism.</p>
<p>RFC 253 - UNIVERSAL::require()</p>
<p><i><a href='http://search.cpan.org/perldoc?Exporter'>Exporter</a></i></p>
</div>
